package com.jmgd.cjlx.controller;


import com.jmgd.cjlx.constants.DzApplicationCommon;
import com.jmgd.cjlx.constants.GyApplicationCommon;
import com.jmgd.cjlx.core.entity.ResultEntity;
import com.jmgd.cjlx.core.enums.ResultEnum;
import com.jmgd.cjlx.dao.JmgdMapper;
import com.jmgd.cjlx.param.dz.CreateOrderParam;
import com.jmgd.cjlx.service.DzGyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

@CrossOrigin
@RestController
@RequestMapping("/gy")
public class GyController {
    private static final Logger logger = LoggerFactory.getLogger(GyController.class);

//    @Autowired
//    DzService dzService;

    @Autowired
    JmgdMapper jmgdMapper;

    @Autowired
    DzGyService dzGyService;


    @PostMapping("/callBack")
    public ResultEntity callBack(@RequestBody Object object) {
        Date now = new Date();

        // 定义日期格式，包括年、月、日、时、分、秒和毫秒
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

        // 将当前时间格式化为字符串
        String formattedDate = sdf.format(now);

        logger.info("回调开始"+formattedDate);
        dzGyService.callBack(object);

        Date nowEnd = new Date();
        String formattedEndDate = sdf.format(nowEnd);
        logger.info("回调结束1"+formattedEndDate);
        return new ResultEntity(ResultEnum.SAVE_SUCCESS,null);
    }

    @RequestMapping("/test")
    public ResultEntity test() throws Exception {
//        dzService.queryPriceBatch();
//        dzService.queryStockBatch();
//        jmgdMapper.updateWarePrice();

        return new ResultEntity(ResultEnum.SUCCESS,null);
    }


    @RequestMapping("/queryPrice")
    public ResultEntity queryPrice(@RequestParam(value = "goodsid") String goodsid) throws Exception {
        return new ResultEntity(ResultEnum.SUCCESS,dzGyService.queryPrice(GyApplicationCommon.APPID,
                GyApplicationCommon.APPSECRET,
                GyApplicationCommon.SUPPLIERID,
                GyApplicationCommon.URL,
                "国盈",
                goodsid));
    }

    @RequestMapping("/queryPriceBatch")
    public ResultEntity queryPriceBatch() throws Exception {
        dzGyService.queryPriceBatch(GyApplicationCommon.APPID,
                GyApplicationCommon.APPSECRET,
                GyApplicationCommon.SUPPLIERID,
                GyApplicationCommon.URL,
                "国盈");
        return new ResultEntity(ResultEnum.SUCCESS,null);
    }

    @RequestMapping("/queryStock")
    public ResultEntity queryStock(@RequestParam(value = "goodsid") String goodsid) throws Exception {
        return new ResultEntity(ResultEnum.SUCCESS,dzGyService.queryStock(
                GyApplicationCommon.APPID,
                GyApplicationCommon.APPSECRET,
                GyApplicationCommon.SUPPLIERID,
                GyApplicationCommon.URL,
                "国盈",
                goodsid));
    }

    @RequestMapping("/queryStockBatch")
    public ResultEntity queryStockBatch() throws Exception {
        dzGyService.queryStockBatch(GyApplicationCommon.APPID,
                GyApplicationCommon.APPSECRET,
                GyApplicationCommon.SUPPLIERID,
                GyApplicationCommon.URL,
                "国盈");
        return new ResultEntity(ResultEnum.SUCCESS,null);
    }


    @PostMapping("/createOrder")
    public ResultEntity createOrder(@RequestBody CreateOrderParam createOrderParam) throws Exception {
        logger.info("创建订单："+createOrderParam.toString());
        return new ResultEntity(ResultEnum.SUCCESS,dzGyService.createOrder(
                GyApplicationCommon.ADDRESSID,
                GyApplicationCommon.SUPPLIERID,
                GyApplicationCommon.APPID,
                GyApplicationCommon.APPSECRET,
                GyApplicationCommon.URL,
                "国盈",
                createOrderParam));
    }

    //界面新建单
    @RequestMapping("/createOrderJm")
    public ResultEntity createOrderJm(@RequestParam(value = "orderCode") String orderCode) throws Exception {
        logger.info("界面创建订单："+orderCode);
        return new ResultEntity(ResultEnum.SUCCESS,dzGyService.createOrderJm(
                GyApplicationCommon.ADDRESSID,
                GyApplicationCommon.SUPPLIERID,
                GyApplicationCommon.APPID,
                GyApplicationCommon.APPSECRET,
                GyApplicationCommon.URL,
                "国盈",
                orderCode));
    }

//    @RequestMapping("/queryOrderStatus")
//    public ResultEntity queryOrderStatus(@RequestParam(value = "orderCode") String orderCode) throws Exception {
//        return new ResultEntity(ResultEnum.SUCCESS,dzGyService.queryOrderStatus(orderCode));
//    }


    @RequestMapping("/heartbeat")
    public ResultEntity heartbeat()  {
        logger.info("正常");
        return new ResultEntity(ResultEnum.SUCCESS,null);
    }

    @RequestMapping("/getPriceQty")
    public ResultEntity getPriceQty() throws Exception {
        logger.info("每天拉取供应商库存和价格");
        dzGyService.queryPriceBatch(DzApplicationCommon.APPID,DzApplicationCommon.APPSECRET,DzApplicationCommon.SUPPLIERID,DzApplicationCommon.URL,"大众");
        dzGyService.queryStockBatch(DzApplicationCommon.APPID,DzApplicationCommon.APPSECRET,DzApplicationCommon.SUPPLIERID,DzApplicationCommon.URL,"大众");

        dzGyService.queryPriceBatch(GyApplicationCommon.APPID,GyApplicationCommon.APPSECRET,GyApplicationCommon.SUPPLIERID,GyApplicationCommon.URL,"国盈");
        dzGyService.queryStockBatch(GyApplicationCommon.APPID,GyApplicationCommon.APPSECRET,GyApplicationCommon.SUPPLIERID,GyApplicationCommon.URL,"国盈");
        jmgdMapper.updateWarePrice();
        return new ResultEntity(ResultEnum.SUCCESS,null);
    }
}
